package com.pzb.framework.web.service;

import com.pzb.common.core.domain.entity.SysUser;
import com.pzb.common.core.domain.model.LoginUser;
import com.pzb.common.core.domain.model.WechatUser;
import com.pzb.wechat.service.IWechatUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 用户验证处理
 *
 * @author pzb
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService
{
    private static final Logger log = LoggerFactory.getLogger(UserDetailsServiceImpl.class);

    @Autowired
    private IWechatUserService userService;

    @Override
    public UserDetails loadUserByUsername(String openid) throws UsernameNotFoundException
    {
        WechatUser user = userService.selectWechatUserById(openid);

        return createLoginUser(user);
    }

    public UserDetails createLoginUser(WechatUser user)
    {
        return new LoginUser(user.getOpenid(), user);
    }
}
